<template>

  <div class="app-container" style="font-size:12px">
    <div class="filter-container">

      <el-date-picker v-model="listQuery.date1" style="width:150px;" format="yyyy-MM-dd" type="datetime" placeholder="订单开始日" />
      <el-date-picker v-model="listQuery.date2" style="width:150px" format="yyyy-MM-dd" type="datetime" placeholder="订单结束日" />
      <el-input v-model="listQuery.customer" placeholder="客户姓名" style="width: 100px;margin-top:6px" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.plates" placeholder="车牌" style="width: 100px;margin-top:6px" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.c_id" placeholder="选择门店" filterable class="filter-item" style="width:150px;margin-top:8px">
        <el-option label="选择门店" value="" />
        <el-option
          v-for="item in company_list"
          :key="item.id"
          :label="item.c_name"
          :value="item.id"
        />
      </el-select>
      <el-select v-model="listQuery.sale_id" placeholder="选择市场人员" filterable class="filter-item" style="width:150px;margin-top:6px">
        <el-option label="选择市场人员" value="" />
        <el-option
          v-for="item in sale_list"
          :key="item.sale_id"
          :label="item.real_name"
          :value="item.sale_id"
        />
      </el-select>
      <el-select v-model="listQuery.shareholder" placeholder="股东" class="filter-item" style="width:130px;margin-top:8px">
        <el-option label="股东" value="" />
        <el-option
          v-for="item in shareholderList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>

      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-top:6px" @click="handleFilter">查询</el-button>
      <!-- <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-top:8px" @click="excelout">导出</el-button> -->
      <excel />
    </div>

    <el-table :key="tableKey" v-loading="listLoading" :data="list" size="mini" border fit highlight-current-row style="width: 100%;margin-top:20px">

      <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-dropdown :hide-on-click="false">

            <span class="el-dropdown-link" style="color:#67C23A">
              更多操作
              <i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <!-- <el-dropdown-item>
                <el-button type="warning" size="mini" @click="deleteOrder(row.order_id)">
                  彻底删除
                </el-button>
              </el-dropdown-item> -->
              <el-dropdown-item>
                <el-button type="success" size="mini" @click="reOrder(row.order_id)">
                  恢复
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button type="info" size="mini" @click="detail(row.order_id)">
                  详情
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button type="success" size="mini" @click="shareholder(row.order_id,row)">
                  股东占比
                </el-button>
              </el-dropdown-item>

            </el-dropdown-menu>

          </el-dropdown>
        </template>
      </el-table-column>
      <el-table-column label="合同编号" align="center" width="180px">
        <template slot-scope="{row}">
          <span>{{ row.contract_num.contract_no }}</span>
        </template>
      </el-table-column>
      <el-table-column label="门店" align="center" width="100px">
        <template slot-scope="{row}">
          <span>{{ row.c_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="首次还款日期" align="center" width="100px">
        <template slot-scope="{row}">
          {{ row.first_payment_day }}
        </template>
      </el-table-column>
      <el-table-column label="租车日期" align="center" width="100px">
        <template slot-scope="{row}">
          {{ row.create_time }}
        </template>
      </el-table-column>
      <el-table-column label="还款周期" align="center" width="100px">
        <template slot-scope="{row}">
          {{ row.days }}/天
        </template>
      </el-table-column>

      <el-table-column label="客户名称" align="center">
        <template slot-scope="{row}">
          <span style="color:dodgerblue;cursor: pointer" @click="plan(row.order_id)">{{ row.real_name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="客户电话" align="center" width="120px">
        <template slot-scope="{row}">
          <span>{{ row.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证" align="center" width="150px">
        <template slot-scope="{row}">
          <span>{{ row.idcard }}</span>
        </template>
      </el-table-column>
      <el-table-column label="购车款" align="center">
        <el-table-column label="总金额" align="center" width="80px">
          <template slot-scope="{row}">
            <span>{{ row.all_price_all }}</span>
          </template>
        </el-table-column>
        <el-table-column label="首款" align="center" width="80px">
          <template slot-scope="{row}">
            <span>{{ row.first_price }}</span>
          </template>
        </el-table-column>
        <el-table-column label="尾款" align="center" width="80px">
          <template slot-scope="{row}">
            <span>{{ row.total_price }}</span>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="租金" align="center">
        <el-table-column label="应收" align="center" width="80px">
          <template slot-scope="{row}">
            <span>{{ row.all_price }}</span>
          </template>
        </el-table-column>
        <el-table-column label="已收" align="center" width="80px">
          <template slot-scope="{row}">
            <span>{{ row.paid_money }}</span>
          </template>
        </el-table-column>
        <el-table-column label="未收" align="center" width="80px">
          <template slot-scope="{row}">
            <span>{{ row.no_paid_money }}</span>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="加融" align="center">
        <el-table-column label="加融期数" align="center" width="80px">
          <template slot-scope="{row}">
            <span>{{ row.jiarong_qs }}</span>
          </template>
        </el-table-column>
        <el-table-column label="加融金额" align="center" width="80px">
          <template slot-scope="{row}">
            <span>{{ row.jiarong_money }}</span>
          </template>
        </el-table-column>
        <el-table-column label="未收加融金额" align="center" width="80px">
          <template slot-scope="{row}">
            <span>{{ row.no_paid_jiarong_qs }}</span>
          </template>
        </el-table-column>
      </el-table-column>

      <el-table-column label="本金" align="center">
        <template slot-scope="{row}">
          <span>{{ row.benjin }}</span>
        </template>
      </el-table-column>

      <el-table-column label="总金额" align="center">
        <template slot-scope="{row}">
          <span>{{ row.total_price }}</span>
        </template>
      </el-table-column>

      <el-table-column label="每期租金" align="center">
        <template slot-scope="{row}">
          <span>{{ row.a_month_price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总期数" align="center" width="70px">
        <template slot-scope="{row}">
          <span>{{ row.total_period }}</span>
        </template>
      </el-table-column>

      <el-table-column label="流量费" align="center">
        <template slot-scope="{row}">
          <span>{{ row.flow }}</span>
        </template>
      </el-table-column>
      <el-table-column label="杂费回款" align="center">
        <template slot-scope="{row}">
          <span>{{ row.incidental_money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支出费用" align="center">
        <template slot-scope="{row}">
          <span>{{ row.outcome_money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="费率" align="center">
        <template slot-scope="{row}">
          <span>{{ row.rate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应收总额" align="center">
        <template slot-scope="{row}">
          <span>{{ row.all_price }}</span>
        </template>
      </el-table-column>

      <el-table-column label="已付期数" align="center">
        <template slot-scope="{row}">
          <span>{{ row.payed_num }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已付金额" align="center">
        <template slot-scope="{row}">
          <span>{{ row.paid_money }}</span>
        </template>
      </el-table-column>

      <el-table-column label="逾期期数" align="center">
        <template slot-scope="{row}">
          <span>{{ row.late_num }}</span>
        </template>
      </el-table-column>

      <el-table-column label="结清日期" align="center">
        <template slot-scope="{row}">
          <span>{{ row.pay_completed_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同支出" align="center">
        <template slot-scope="{row}">
          <span>{{ row.contract_outcome }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同收入" align="center">
        <template slot-scope="{row}">
          <span>{{ row.contract_income }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品" align="center">
        <template slot-scope="{row}">
          <span>{{ row.goods_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="进件人" align="center">
        <template slot-scope="{row}">
          <span>{{ row.sale_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="过户/抵押公司" align="center" width="200">
        <template slot-scope="{row}">
          <span>{{ row.onwer_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="初审" align="center" width="70">
        <template slot-scope="{row}">
          <span>{{ row.first_admin_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="中审" align="center" width="70">
        <template slot-scope="{row}">
          <span>{{ row.second_admin_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="制单" align="center" width="70">
        <template slot-scope="{row}">
          <span>{{ row.third_admin_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="终审" align="center" width="70">
        <template slot-scope="{row}">
          <span>{{ row.fourth_admin_name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="当前状态" align="center">
        <template slot-scope="{row}">
          <span>{{ row.status_name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="备注信息" align="center" width="200">
        <template slot-scope="{row}">
          <span>{{ row.descript }}</span>
        </template>
      </el-table-column>

    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <el-dialog title="租金计划" :visible.sync="dialogPlanAble" style="width:1500px;margin:auto">
      <table class="flowtable">
        <tr>
          <th>期数</th>
          <th>应付日期</th>
          <th>应付金额</th>
          <th>实付金额</th>
          <th>实付日期</th>
          <th>逾期天数</th>
          <th>付租状态</th>
        </tr>
        <tr v-for="(item,index) in plan_list" :key="index">
          <td>{{ item.num }}</td>
          <td>{{ item.time_date }}</td>
          <td>{{ item.total_money }}</td>
          <td>{{ item.paid_money }}</td>
          <td>{{ item.sh_time }}</td>
          <td>{{ item.late_num }}</td>
          <td>{{ item.status_name }}</td>
        </tr>
      </table>
      <pagination v-show="plan_total>0" :total="plan_total" :page.sync="planQuery.page" :limit.sync="planQuery.limit" @pagination="planList" />
    </el-dialog>

    <el-dialog title="股东占比" :visible.sync="shareholderAble" style="width:2000px;margin:auto">
      <el-form ref="dataForm" :inline="true" :model="shareholder_from" label-width="80px">
        <div v-for="(item,k) in shareholder_from.datas" :key="k" style="border-bottom: 1px solid #eee;height: 50px;padding-top: 7px;">
          <el-form-item label="股东">
            <el-select v-model="item.shareholder" placeholder="股东" style="width: 80px">
              <el-option
                v-for="item in shareholderList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="本金">
            <el-input v-model="item.money" placeholder="本金(元)" style="width: 80px" />
          </el-form-item>
          <el-form-item label="占比">
            <el-input v-model="item.percentage" placeholder="1-10成" style="width: 80px" />
          </el-form-item>
          <el-form-item label="月供分利">
            <el-input v-model="item.benefits" placeholder="1-10成" style="width: 80px" />
          </el-form-item>
          <el-form-item>
            <el-button v-if="k==0" type="primary" size="mini" @click="addDocument()">+</el-button>
            <el-button v-else type="info" size="mini" @click="delDocument(k)">X</el-button>
          </el-form-item>
        </div>
      </el-form>

    </el-dialog>

  </div>

</template>

<script>
import excel from '@/components/excelout/index.vue'
import {
  channelBankList, submmitChannel
} from '@/api/channel'
import {

  financeOrderList, deleteOrder, getShareHolder, reOrderSubmit, paymentList, getThisOrderShareHolder,exceloutfinance

} from '@/api/finance'
import { formatDate } from '@/utils/formatDate.js'
import { merchantListOnlyMerchant } from '@/api/merchant'
import { fetchList } from '@/api/sale'
import waves from '@/directive/waves' // waves directive
import {
  parseTime
} from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'ComplexTable',
  components: {
    Pagination, excel
  },
  directives: {
    waves
  },
  filters: {
   	formatDate(time) {
   	        time = time * 1000
   	        const date = new Date(time)

   	        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
   	      }
  },
  data() {
    return {
      plan_total: 0,
      shareQuery: {
        limit: 100
      },
      menu_flag: 1,
      shareholderAble: false,

      plan_list: [],
      dialogPlanAble: false,
      status_form: {
        status: 0
      },
      shareholderList: [],
      other_form: {
        order_id: '',
        money: '',
        desc: '',
        incidental_money: '',
        outcome_money: '',
        status: '0',
        contract_outcome: 0,
        contract_income: 0,
        contract_outcome_date: '',
        contract_income_date: ''
      },
      shareholder_from: {
        order_id: '',
        datas: [
          {
            shareholder: '',
            money: '',
            percentage: '',
            benefits: ''
          }
        ]
      },
      changeStatusAble: false,
      tableKey: 0,
      shareholderAble: false,
      descAble: false,
      outcomeInputAble: false,
      incidentalAble: false,
      tableRecord: 1,
      list: null,
      record_list: null,
      listRecordLoading: true,
      recordTotal: 0,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        customer: '',
        c_id: '',
        sale_id: '',
        order_status: 5,
        excel: 0,
        flag: 1,
        date1: '',
        date2: '',
        shareholder: ''
      },
      listMerchantQuery: {
        page: 1,
        limit: 500
      },
      company_list: [],
      sale_list: [],
      form: [],
      planQuery: {
        page: 1,
        limit: 10
      }
    }
  },
  created() {
    this.getCompanyList()
    this.getList()
    this.getShareHolder()
    // console.log(menu_flag);
  },

  methods: {
    plan(order_id) {
      this.dialogPlanAble = true
      this.planQuery.order_id = order_id
      this.planList()
    },
    planList() {
      paymentList(this.planQuery).then(response => {
        this.plan_list = response.data.list.data
        this.plan_total = response.data.list.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listLoading = true
      this.listQuery.page = 1
      this.getList()
    },
    shareholder(order_id, row) {
      this.shareholderAble = true
      this.shareholder_from.order_id = order_id
      this.getShareHolder(order_id)
    },
    getShareHolder(order_id) {
      getShareHolder(this.shareQuery).then(response => {
        this.shareholderList = response.data.data
      })
      this.getThisOrderShareHolder(order_id)
    },
    getThisOrderShareHolder(order_id) {
      const pam = { order_id: order_id }
      getThisOrderShareHolder(pam).then(response => {
        // this.thisOrderShareholder = response.data.data;
        this.shareholder_from.datas = [
          {
            shareholder: '',
            money: '',
            percentage: '',
            benefits: ''
          }
        ]
        if (response.data.data.length > 0) {
          this.shareholder_from.datas = response.data.data
        }
      })
    },
    deleteOrder(order_id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const pam = { order_id: order_id }
        deleteOrder(pam).then(response => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    reOrder(order_id) {
      this.$confirm('确定要恢复该订单吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const pam = { order_id: order_id }
        reOrderSubmit(pam).then(response => {
          this.$message({
            type: 'success',
            message: '恢复成功!'
          })
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    detail(order_id) {
      this.$router.push('/order/detail/' + order_id)
    },
    getCompanyList() {
      merchantListOnlyMerchant(this.listMerchantQuery).then(response => {
        this.company_list = response.data.company
        this.sale_list = response.data.sale.data
      })
    },
    excelout() {
      this.listLoading = true
      this.loadingtext = '正在生成表格，请稍等...'
      this.listQuery.page = 1
      this.listQuery.limit = this.total
      exceloutfinance(this.listQuery).then(response => {
        this.listLoading = false
        const filepath = response.data.path
        window.location.href = filepath
      })
    },
    getList() {
      financeOrderList(this.listQuery).then(response => {
        if (!this.listQuery.excel) {
          this.list = response.data.list.data
          this.total = response.data.list.total
          this.allcount = response.data.countall

          setTimeout(() => {
            this.listLoading = false
          }, 0.1 * 1000)
        } else {
          this.listQuery.excel = 0
        }
      })
    }
  }
}
</script>
<style>
.collectcss{margin-top:7px;color:#999;font-size:14px}
.el-table .warning-row {
      background: orangered;
    }

  .el-table .success-row {
    background: #98dd72;
  }
  .item {
  margin-top: 10px;
}
.flowtable{
    width: 80%;margin-left: 10%;
    border: 1px solid #eee;margin-top: 20px;  border-collapse:collapse;
  }
  .flowtable tr {height: 40px;}
  .flowtable td,.flowtable th{
    text-align: center;
    border-collapse:collapse;border: 1px solid #eee;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    white-space: nowrap;
  }
  td{padding: 0;}
  select{
    width:100px;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    float: left;
  }
   .el-table .warning-row {
      background: orangered;
    }
    .el-table .tips-row {
      background:goldenrod;
    }

  .el-table .success-row {
    background: #98dd72;
  }
  .el-table .default-row {
    background: #ccc;
  }
</style>
